package org.rsbot.util;

import java.awt.Window;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class UpdateUtil
{
  private static final Logger log = Logger.getLogger(UpdateUtil.class.getName());
  private final Window parent;

  public UpdateUtil(Window paramWindow)
  {
    this.parent = paramWindow;
  }

  public void checkUpdate(boolean paramBoolean) {
    if (paramBoolean) {
      log.info("Checking for update...");
    }
    if (getLatestVersion() > GlobalConfiguration.getVersion()) {
      log.info("New version available!");
      int i = JOptionPane.showConfirmDialog(this.parent, "A newer version is available. Do you wish to update?", "Update Found", 0);

      if (i != 0) {
        log.info("Cancelled update");
      }
      if (i == 0) {
        updateBot();
      }
    }
    else if (paramBoolean) {
      log.info("You have the latest version");
    }
  }

  public static boolean download(String paramString1, String paramString2)
  {
    BufferedOutputStream localBufferedOutputStream = null;

    InputStream localInputStream = null;
    try {
      URL localURL = new URL(paramString1);

      localBufferedOutputStream = new BufferedOutputStream(new FileOutputStream(paramString2));
      URLConnection localURLConnection = localURL.openConnection();
      localInputStream = localURLConnection.getInputStream();

      byte[] arrayOfByte = new byte[1024];
      int j;
      while ((j = localInputStream.read(arrayOfByte)) != -1)
        localBufferedOutputStream.write(arrayOfByte, 0, j);
      return true;
    }
    catch (Exception localException)
    {
      log.info("Downloading failed!");
      int i = 0;
      return true;
    }
    finally
    {
      try
      {
        if (localInputStream != null) {
          localInputStream.close();
        }
        if (localBufferedOutputStream != null)
          localBufferedOutputStream.close();
      }
      catch (IOException localIOException3) {
        log.info("Downloading failed!");
        return false;
      }
    }
    return true;
  }

  private int getLatestVersion()
  {
    try
    {
      InputStream localInputStream = new URL("http://links.powerbot.org/version").openConnection().getInputStream();

      int i = 0;
      byte[] arrayOfByte = new byte[2];
      while (i += localInputStream.read(arrayOfByte, i, 2 - i) != 2);
      return ((0xFF & arrayOfByte[0]) << 8) + (0xFF & arrayOfByte[1]);
    } catch (IOException localIOException) {
      log.info("Unable to download latest version information");
    }return -1;
  }

  private void updateBot()
  {
    log.info("Downloading update...");

    String str1 = "RSBot-" + getLatestVersion() + ".jar";

    download("http://links.powerbot.org/update", str1);

    String str2 = "RSBot-" + GlobalConfiguration.getVersion() + ".jar";

    Runtime localRuntime = Runtime.getRuntime();
    try
    {
      localRuntime.exec("java -jar " + str1 + " delete " + str2);
      System.exit(0);
    }
    catch (IOException localIOException)
    {
    }
  }
}